package com.adcc.ags.gms.gw.service;

import com.adcc.ags.gms.core.entity.GWLogLevel;
import com.adcc.ags.gms.core.util.CacheStack;
import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.gw.vo.QueueInfoVO;
import com.adcc.ags.gms.gw.vo.UserMonitorVO;
import com.adcc.ags.gms.mq.service.QueueService;
import com.adcc.ags.gms.mq.vo.QueueVO;
import com.adcc.ags.gms.user.entity.GWUser;
import com.adcc.ags.gms.user.service.GWUserService;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Stack;

@Service("QueueMonitorService")
public class QueueMonitorServiceImpl implements QueueMonitorService {

    @Autowired
    private QueueService queueService;

    @Autowired
    private GWUserService gwUserService;

    @Autowired
    private LogFactory log;

    /**
     * 取得用户信息
     * @return
     */
    private String getUserInfo(long queueId){
        StringBuilder sb = new StringBuilder();
        List<GWUser> list = gwUserService.findByQueueId(queueId);
        if(list != null && list.size() > 0){
            for(int i = 0;i < list.size();i++){
                if(i == list.size()-1){
                    sb.append(list.get(i).getUserName());
                }else{
                    sb.append(list.get(i).getUserName() + ",");
                }
            }
        }
        return sb.toString();
    }

    /**
     * 取得系统队列信息
     * @return
     */
    private List<QueueInfoVO> getSysQueueInfo(){
        try{
            List<QueueInfoVO> result = Lists.newArrayListWithExpectedSize(1000);
            Optional<List<QueueVO>> optional = queueService.findAllSysQueue();
            if(optional.isPresent()){
                if(optional.get().size() > 0){
                    for(QueueVO queue : optional.get()){
                        QueueInfoVO vo = new QueueInfoVO(queue);
                        vo.setUser("Gateway");
                        result.add(vo);
                    }
                }
                return result;
            }else{
                return null;
            }
        }catch (Exception ex){
            log.error(QueueMonitorService.class, "MQ", GWLogLevel.SERIOUS_ALARM, "获取系统队列信息失败", ex);
            return null;
        }
    }

    /**
     * 取得用户队列信息
     * @return
     */
//    private List<QueueInfoVO> getUserQueueInfo(){
//        try{
//            List<QueueInfoVO> result = Lists.newArrayListWithExpectedSize(1000);
////            Optional<List<QueueVO>> optional = queueService.findAllSourceUserQueue();
////            if(optional.isPresent()){
////                if(optional.get().size() > 0){
////                    Optional<List<QueueVO>> opt = queueService.findUserQueueByEffect(true);
////                    if(opt.isPresent() && opt.get().size() > 0){
////                        for(QueueVO q : opt.get()){
////                            for(QueueVO queue : optional.get()){
////                                if(queue.getName().equals(q.getName())){
////                                    QueueInfoVO vo = new QueueInfoVO(queue);
////                                    String strUserInfo = getUserInfo(q.getId());
////                                    vo.setUser(strUserInfo);
////                                    result.add(vo);
////                                }
////                            }
////                        }
////                    }
////                }
////                return result;
////            }else{
////                return null;
////            }
//            Optional<List<QueueVO>> optional = queueService.findAllSourceUserQueue();
//            if(optional.isPresent()){
//                Stack<List<UserMonitorVO>> stack = CacheStack.getUserMonitorStack();
//                if(!stack.peek().isEmpty()){
//                    List<UserMonitorVO> list = stack.peek();
//                    for(QueueVO queueVO : optional.get()){
//                        QueueInfoVO vo = new QueueInfoVO(queueVO);
//                        for(UserMonitorVO umv : list){
//                            if(umv.getRecQueueName().equals(queueVO.getName())){
//                                vo.setUser(umv.getName());
//                                vo.setEnqueued(umv.getDownMsgCount());
//                                break;
//                            }
//                            if(umv.getSendQueueName().equals(queueVO.getName())){
//                                vo.setUser(umv.getName());
//                                vo.setEnqueued(umv.getUpMsgCount());
//                                break;
//                            }
//                        }
//                        result.add(vo);
//                    }
//                }
//                return result;
//            }else{
//                return null;
//            }
//        }catch (Exception ex){
//            log.error(QueueMonitorService.class, "MQ", GWLogLevel.SERIOUS_ALARM, "获取用户队列信息失败", ex);
//            return null;
//        }
//    }

        private List<QueueInfoVO> getUserQueueInfo(){
        try{
            List<QueueInfoVO> result = Lists.newArrayListWithExpectedSize(1000);

            // 获取源MQ用户队列
            Optional<List<QueueVO>> optional = queueService.findAllSourceUserQueue();
            if(optional.isPresent() && optional.get().size() > 0){
                List<QueueVO> list = optional.get();
                for(QueueVO vo : list){
                    QueueInfoVO q = new QueueInfoVO(vo);
                    if(!CacheStack.getUserMonitorStack().isEmpty()){
                        List<UserMonitorVO> userMonitorVOList = CacheStack.getUserMonitorStack().peek();
                        if(userMonitorVOList != null && userMonitorVOList.size() > 0){
                            for(UserMonitorVO umv : userMonitorVOList){
                                if(q.getName().equals(umv.getRecQueueName())){
                                    q.setUser(umv.getName());
                                    q.setEnqueued(umv.getDownMsgCount());
                                    break;
                                }
                                if(q.getName().equals(umv.getSendQueueName())){
                                    q.setUser(umv.getName());
                                    q.setEnqueued(umv.getUpMsgCount());
                                    break;
                                }
                            }
                        }
                    }
                    result.add(q);
                }
            }
            Collections.sort(result);
            return result;
        }catch (Exception ex){
            log.error(QueueMonitorService.class, "MQ", GWLogLevel.SERIOUS_ALARM, "获取用户队列信息失败", ex);
            return null;
        }
    }

    @Override
    public List<QueueInfoVO> getQueueInfo() {
        List<QueueInfoVO> result = Lists.newArrayListWithExpectedSize(1000);
        try {

            // 取得系统用户队列信息
            List<QueueInfoVO> sysQueueInfo = getSysQueueInfo();
            if(sysQueueInfo != null && sysQueueInfo.size() > 0){
                for(QueueInfoVO vo : sysQueueInfo){
                    result.add(vo);
                }
            }

            // 取得用户队列信息
            List<QueueInfoVO> userQueueInfo = getUserQueueInfo();
            if(userQueueInfo != null && userQueueInfo.size() > 0){
                for(QueueInfoVO vo : userQueueInfo){
                    result.add(vo);
                }
            }
        } catch (Exception ex) {
            log.error(QueueMonitorService.class, "MQ", GWLogLevel.SERIOUS_ALARM, "获取消息队列信息失败", ex);
        }
        return result;
    }
}
